package com.sbtr.business.utils;

import com.sbtr.business.system.entity.DictionaryParam;
import com.sbtr.business.system.service.DictionaryParamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

/**
 *
 */
@Component
public class AnalyzeUtil {
    @Autowired
    DictionaryParamService dictionaryParamService;

    private final static ConcurrentHashMap<String, String> dictionary = new ConcurrentHashMap<>();

    @PostConstruct
    public void init() {//初始化加载字典
        List<DictionaryParam> list = dictionaryParamService.list();
        for (DictionaryParam dictionaryParam : list) {
            dictionary.put(dictionaryParam.getOptName(), dictionaryParam.getOptCode());
        }
    }

    public void flushDictionary() {
        dictionary.clear();
        init();
    }


    //解析字符串获取字典code，后面可添加解析类型
    public String analyze(String n, int type) {
        if (dictionary.size() == 0) {
            init();
        }
        switch (type) {
            case 1:
            default://1或默认解析字符串类型
                return dictionary.get(analyzeChar(n));
        }
    }

    public String analyzes(List<String> list, int type) {
        if (dictionary.size() == 0) {
            init();
        }
        switch (type) {
            case 1:
            default://1或默认解析字符串类型
                Set<String> filter= new HashSet<>();
                for (int i = 0; i < list.size(); i++) {
                    if(StringUtils.isNotBlank(list.get(i))){
                        filter.add(analyzeChar(list.get(i)));
                    }
                }

                //优先级别,和解析不同，选最通用解 字符型>日期时间型>日期型>浮点型>整数型>
                if(filter.contains("字符型")) return "字符型";
                if(filter.contains("日期时间型")) return "日期时间型";
                if(filter.contains("日期型")) return "日期型";
                if(filter.contains("浮点型")) return "浮点型";
                if(filter.contains("整数型")) return "整数型";
                return "字符型";
        }
    }

    //解析字符串类型字典
    public String analyzeChar(String n) {
        if (isDateTime(n))return "日期时间型";
        else if (isDate(n)) return "日期型";
        else if (isFloat(n)) return "浮点型";
        else if (isInteger(n)) return "整数型";
        else return "字符型";

    }

    private boolean isInteger(String n) {
        Pattern pattern = Pattern.compile("^-?[0-9]{1,}$");
        return pattern.matcher(n).find();
    }

    private boolean isFloat(String n) {
        Pattern pattern = Pattern.compile("^-?[0-9]+\\.([0-9]+)?$");
        return pattern.matcher(n).find();
    }

    private boolean isDate(String n) {
        Pattern pattern = Pattern.compile("^\\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])");
        return pattern.matcher(n).find();
    }

    private boolean isDateTime(String n) {
        Pattern pattern = Pattern.compile("^\\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])\\s([0-1][0-9]|2[0-4]):[0-5][0-9]:[0-5][0-9]");
        return pattern.matcher(n).find();
    }
}
